/**
* 
*	ReceManageAction类
*	作者：邓磊
*	时间：2016年7月4日 16:40:43
*	作用：提供地址管理
*	域：/weixin/user
*
*/
package com.eptok.weixin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.eptok.weixin.util.WxCooikeManager;

import hikefa.core.exception.BizException;
import hikefa.core.remote.client.RemoteCall;
import yspos.cpfs.express.entity.ExpressAddr;
import yspos.cpfs.remoteserver.iface.IExpressService;
import yspos.cpfs.remoteserver.iface.YsServerUtil;

@Controller
@RequestMapping("/weixin/user")
public class ReceManageAction extends BaseDispatchAction{
	
	private static final Logger log = Logger.getLogger(ReceManageAction.class);
	private static final int numAddr = 15;
	/**
	 * 获得用户的收件地址信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = { "/findReceAddr.do" })
	public String findReceAddr(HttpServletRequest request, HttpServletResponse response){
		try{
			IExpressService iexpressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
			List addrs = iexpressServer.getAddrByUID(WxCooikeManager.getUser(request).getId());
			request.setAttribute("addrs", addrs);
		}catch(Exception e){
			e.printStackTrace();
			throw new BizException();
		}
		return PATH+"/ReceManage/index";
	}
	
	/**
	 * @Description 获取地址明细
	 * @param request
	 * @param response
	 * @param SN
	 * @return
	 */
	@RequestMapping(value = {"/receDetail.do"})
	public ModelAndView receDetail(HttpServletRequest request, HttpServletResponse response,
			String SN){
		if(SN==null || SN.trim().length()==0){
			toFail(request, "1", "无效编号");
		}
		int flag = 0;
		try{
			IExpressService iexpressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
			ExpressAddr addr = iexpressServer.getAddrBySN(SN);
	
			request.setAttribute("provjson", YsServerUtil.getProvJson());//所有的省全部加进去
			request.setAttribute("cityjson", YsServerUtil.getCityJson());//所有的市全部加进去
			
			request.setAttribute("addr", addr);
			request.setAttribute("flag", flag);
		}catch(Exception e){
			e.printStackTrace();
			throw new BizException();
		}
		return new ModelAndView(PATH+"/ReceManage/detail");
	}
	/**
	 * @Description 删除单个地址
	 * @param request
	 * @param response
	 * @param SN
	 * @param usercode
	 * @return
	 */
	//@SuppressWarnings("unchecked")
	@RequestMapping(value = {"/deteAddr.do"})
	public ModelAndView deteAddr(HttpServletRequest request, HttpServletResponse response,
			String SN){
		try{
			IExpressService iexpressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
			if(SN==null || SN.trim().length()==0){ //SN为空
				return toFail(request, "1", "系统错误");
			}
			ExpressAddr addr = iexpressServer.getAddrBySN(SN);
			if(!addr.getUsercode().equals(WxCooikeManager.getUser(request).getId()))
			{
				return toFail(request, "1", "系统错误");
			}
			iexpressServer.deteAddr(SN);
		}catch(Exception e){
			throw new BizException();
		}
		return new ModelAndView("redirect:findReceAddr.do");
	}
	
	/**
	 * @Description 更新地址
	 * @param request
	 * @param response
	 * @param expressAddr
	 * @param usercode
	 * @return
	 */
	@RequestMapping(value = {"/updateAddr.do"})
	public ModelAndView updateAddr(HttpServletRequest request, HttpServletResponse response,
			ExpressAddr expressAddr){
		String usercode = WxCooikeManager.getUser(request).getId();
		if(expressAddr==null){
//			return "../pages/wx/error.jsp";
			return toFail(request, "1", "111");
		}
		expressAddr.setUsercode(usercode);
		IExpressService iexpressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
		try{

			expressAddr.setProvince(YsServerUtil.getAreaName(expressAddr.getProCode()));
			expressAddr.setCity(YsServerUtil.getAreaName(expressAddr.getCityCode()));
			
			if("on".equals(expressAddr.getIsDefault())){
				expressAddr.setIsDefault("00");   //默认
				iexpressServer.upDefaultAddr(usercode); //全部设为非默认
			}else{
				expressAddr.setIsDefault("01");   //非默认
			}
			if(expressAddr.getPostcode()==null){
				expressAddr.setPostcode(""); //默认邮编
			}
			expressAddr.setReceiveName(expressAddr.getReceiveName().trim());
			expressAddr.setAddressDetail(expressAddr.getAddressDetail().trim());
			
			iexpressServer.updateAddr(expressAddr);
			
		}catch(Exception e){
			e.printStackTrace();
			throw new BizException();
		}
		return new ModelAndView("redirect:findReceAddr.do");
	}
	
	/**
	 * @Description 新增地址
	 * @param request
	 * @param response
	 * @param expressAddr
	 * @param usercode
	 * @return
	 */
	//@SuppressWarnings("rawtypes")
	@RequestMapping(value = {"/preAddr.do"})
	public Object preAddr(HttpServletRequest request, HttpServletResponse response,
			ExpressAddr expressAddr,String rs){
		log.debug("addAddr");
		String usercode = WxCooikeManager.getUser(request).getId();
		if(expressAddr==null){
			return toFail(request, "1", "11");
		}
		IExpressService iexpressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
		try{
			expressAddr.setProvince(YsServerUtil.getAreaName(expressAddr.getProCode()));
			expressAddr.setCity(YsServerUtil.getAreaName(expressAddr.getCityCode()));
			
			if("on".equals(expressAddr.getIsDefault())){
				expressAddr.setIsDefault("00");   //默认地址
				iexpressServer.upDefaultAddr(usercode); //全部设为非默认
			}else{
				expressAddr.setIsDefault("01");   //非默认
			}
			if(expressAddr.getPostcode()==null){
				expressAddr.setPostcode(""); //默认邮编
			}
			expressAddr.setUsercode(usercode);
			expressAddr.setReceiveName(expressAddr.getReceiveName().trim());
			expressAddr.setAddressDetail(expressAddr.getAddressDetail().trim());
			
			//收件地址不能超过15个
			if(iexpressServer.getAddrByUID(usercode).size()<numAddr){  
				expressAddr = iexpressServer.saveAddr(expressAddr);
			}
			else{ 
				toFail(request, "1", "超过地址设置上线");
			}
		}catch(Exception e){
			throw new BizException();
		}
		if(rs != null && rs.length() > 0)
		{
			//将刚刚保存的地址放入cookie
			WxCooikeManager.setCookie(response, "order."+rs+"Id", expressAddr.getSn(), 60*60*24);
			return new ModelAndView("redirect:expressSend.do");
		}else
		{
			return new ModelAndView("redirect:findReceAddr.do");
		}
	}
	
	/**
	 * @Description 调转新增界面
	 * @param request
	 * @param response
	 * @param usercode
	 * @return
	 */
	@RequestMapping(value = {"/addView.do"})
	public ModelAndView addView(HttpServletRequest request, HttpServletResponse response,String rs){
		int flag = 1;
		
		request.setAttribute("provjson", YsServerUtil.getProvJson());//所有的省全部加进去
		request.setAttribute("cityjson", YsServerUtil.getCityJson());//所有的市全部加进去
		
		request.setAttribute("flag", flag);
		request.setAttribute("rs", rs);
		return new ModelAndView(PATH+"/ReceManage/detail");
	}
}
